<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%
	String filePath = request.getParameter("filePath");
	if(filePath==null){
		filePath="";
	}
 %> 
<script type="text/javascript" src="<%=path %>/resource/lib/plugins/ajaxfileupload.js"></script>
<input type="hidden" id="filePath" value="<%=filePath %>"/>
<script type="text/javascript">
  
      var upload_result_params = []; 
   	  var serverPrefixPath = "";
      
   	//页面加载
  	$(function(){
  		//添加上传组件
  		$(".upfileBoxs").each(function(index, el){
      		$(this).append(addUpfileBox());
      	});
  		//获取服务器路径前缀
  		$.ajax({
  			type: 'get',
  			url: '<%=path%>/system/sysulfilesrec/getServerPath.do',
  			success:function(msg){
  				serverPrefixPath = msg;
  				console.log(serverPrefixPath);
  			}
  		});
  	});
      
      $("#btnUpload2").click(function() {
    	   	ajaxFileUploadImg();
      });
     var FileCount=1;
  	//添加上传组件方法
  	function addUpfileBox() {
  		$Html=CreatUploadInputBox({
  			id:"uploads"+FileCount,  //id跟着序号走
  			clearLine: true,
  			deletable: false,
  			finishClear: true //上传成功后清空文本框
  		});
  		FileCount++;
  		return $Html;
  	}
      
     /*  var FileCount=1;
      $("#add_upfileBox2").click(function(event) {
    	  if(FileCount>10){
    		  $.ligerDialog.SQalert({content: "超出最大上传数量" , SQ_type: "warning" });
    		  return;
    	  }
          $Html=CreatUploadInputBox({
              id:"uploads"+FileCount,  //id不跟着序号走！
              clearLine: true,
              deletable: true,
            //  finishClear: true, //上传成功后清空文本框
              onDelete: function  (upfileBox) {
            	  FileCount--;
                  setUpfileCount (); //删除的时候重新设置序号
                  var delFileName = upfileBox.find('[type=file]')[0].files[0].name;
                  var temp;
                  for(temp = 0; temp < upload_result_params.length;temp++){
                	  if(upload_result_params[temp].fileNm == delFileName){
                		  upload_result_params.splice(temp,1);
                		  break;
                	  }
                  }
              },
              onChange: function (e) {
                  $(e.target).remove_afterUploadIcon();//删除时,移除已上传文件的下载按钮
              }
          });
          $Html.prepend('<span class="UpfileCount"></span>');
          $(".upfileBoxs").append($Html);
          //$Html.insertBefore($(this));
          //$Html.appendTo($("#more"));
          setUpfileCount (); //新增组件后，设置序号
          FileCount++;
      }); */
     /*  function setUpfileCount () { //设置序号
          $(".UpfileCount").each(function(index, el) {
              $(this).html(index+1);
          });
      	  
      } */
      	
        
        function ajaxFileUploadImg(){  
            //获取file的全部id  
            var uplist = $("input[id^=uploads]"); 
            //alert(JSON.stringify(uplist));
            if(uplist.length==0){
            	$.ligerDialog.SQalert({content: "没有要上传的内容！" , SQ_type: "warning" });
            	return;
            }
	        var arrId = [];
	        var fileNames = [];//用于判断是否有重名文件
	        for (var i=0; i< uplist.length; i++){  
	            if(uplist[i].value){  
	                arrId[i] = uplist[i].id; 
	                if($.inArray(uplist[i].value, fileNames) >= 0){
	                	$.ligerDialog.SQalert({content: uplist[i].value+"文件重名！" , SQ_type: "warning" });
	                	return;
	                }
	                fileNames[i] = uplist[i].value;
	            }  
            }
	        var _filePath = $("#filePath").val();
       		var wait_dialog=$.ligerDialog.SQalert({content: '上传中,请稍后...' ,SQ_type: "waitting" });
	        $.ajaxFileUpload({  
	            url:'<%=path%>/system/sysulfilesrec/upload.do',
	            secureuri:false,  
	            fileElementId: arrId,  //这里不在是以前的id了，要写成数组的形式！  
	            dataType: 'json',  
	            data: {filePath:_filePath},  
	            success: function (msg){ 
	            	wait_dialog.close();
	            	if(msg.success == 1){
	                    $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
		            	//alert(JSON.stringify(msg.data));
	            		upload_result_params = upload_result_params.concat(msg.data);
	            		
	            		uploadCallbackForMore(msg.data);
	            		
	            		showFiles(msg.data);
	            		//遍历msg.data 为filePath加上服务器路径前缀
	            		
	            		if(serverPrefixPath.length >1 && serverPrefixPath.indexOf("\"")>=0){
	            			serverPrefixPath = serverPrefixPath.substring(1,serverPrefixPath.length-1);
	            		}
	            		var uploadJOSN = JSON.parse(JSON.stringify(msg.data));;
	            		for (var i=0; i< uploadJOSN.length; i++){  
	            			var oldPath = "";
	            			var newPath = "";
	            			oldPath = uploadJOSN[i].filePath;
	            			newPath = serverPrefixPath + oldPath;
	            			uploadJOSN[i].filePath = newPath;
	            		}
	            		
	            		//上传成功后在文件后加入下载按钮
	            		$('.upfileBoxs [type=file]').each(function(index, el) {
	                        $(el).afterUploadFile({
	                            filenameField: 'fileNm', //默认值就是fileNm
	                            filePathField: 'filePath', //默认值就是filePath
	                            files_DATA: uploadJOSN
	                        });
	                    });
	                } else{
	                    $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
	                }
	            },
	            complete: function(){
	            	
	            }
	        });  
    	}  
        //获取上传结果参数
        function getUploadResultParams(){
        	return upload_result_params;
        }
        //清除上传结果参数
        function clearUploadResultParams(){
        	//移除文件上传标签
        	$(".fileUploadBox").remove();
        	upload_result_params = [];//设置参数为空
        	FileCount = 1;
        	$("#add_upfileBox2").click();
        }
        
        //上传回调方法
        function uploadCallbackForMore(_data){

        }
    
</script>
